<?php

namespace app\adm\controller;
use think\Db;
class Mall extends Base
{

	// 商家列表
/*	public function shop()
	{
		$map['is_shop'] = 1;
		$order = '';
		if (is_get()) {
			if (input('get.keywords')) {
				$map['name|tel'] = ['like','%'.input('get.keywords').'%'];
			}
			if (is_numeric(input('get.status'))) {
				$map['status'] = input('get.status');
			}

			if (input('get.pid')) {
				$map['pid'] = input('get.pid');
			}

			if (input('get.province')) {
				$map['province'] = input('get.province');
			}

			if (input('get.city')) {
				$map['city'] = input('get.city');
			}

			if (input('get.order')) {
				$order = input('get.order').' DESC';
			} else {
				$order = 'user.create_time DESC';
			}
		}

		$list = model('User')->getList($map);

		$this->assign('list', $list);

		return $this->fetch();
	}*/

    // 商品列表
    public function goods()
    {
        if (is_post()) {
			$post = input('post.');
			if (input('get.act') == 'opt') {
				return model('Goods')->modifyStatus($post['id'], $post);
			}
			// 设置热销
			if (input('get.act') == 'hot') {
				if (db('goods')->where('id', $post['id'])->setField('is_hot', $post['status']))
					$this->success('操作完成');
				$this->error('稍后重试');
			}
		}

		$map = [];
		// $map['status'] = 1;
		if(is_get()){
			if(is_numeric(input('get.status'))){
				$map['status'] = input('get.status');
			}
			if(input('get.cate_id')){
				$map['cate_id'] = input('get.cate_id');
			}
			if(input('get.shop_id')){
				$map['shop_id'] = input('get.shop_id');
			}
			if(input('get.keywords')){
				$map['goods_name'] = ['like','%'.input('get.keywords').'%'];
			}
		}

		$cate = db('goods_cate')->select();
		// foreach ($cate as $k => $v) {
		// 	$cate[$k]['son'] = db('goods_cate')->where('pid',$v['id'])->select();
		// }
		$this->assign('cate',$cate);

		$this->assign('list',model('Goods')->getList($map));
		return $this->fetch();
    }
    
    // 商品分类
	public function cate()
	{
		// if(input('get.id')){
		// 	return json(db('GoodsCate')->where('id',input('get.id'))->find());
		// }
		// if(is_post()){
		// 	if (input('post.id')) {
		// 		$rst = model('GoodsCate')->editInfo(input('post.'));
		// 	} else {
		// 		$rst = db('GoodsCate')->insert(input('post.'));
		// 	}
		// 	if($rst){
		// 		$this->success('操作成功', '', '', 0);
		// 	}
		// 	$this->error('操作失败');
		// }

		// $levelOne = db('goods_cate')
		// 	-> where('pid',0)
		// 	-> order('level ASC')
		// 	-> paginate()
		// 	-> each(function ($item, $key) {
		// 		$item['son'] = $item['id'];
		// 		$item['son'] =db('goods_cate')
		// 			-> where('pid',3)
		// 			-> order('level ASC')
		// 			-> select();
		// 		return $item;
		// 	});
		$levelOne = db('goods_cate')
			-> where('pid',0)
			-> order('level ASC')
			-> paginate();
	// halt($levelOne);
		$this->assign('levelOne', $levelOne);
		return $this->fetch();
	}

	// 添加修改商品信息
	public function addGoods()
	{
		if(is_post()){
			$post = input('post.');
			$validate = validate('Vgoods');
			$rst = $validate->check($post);
			if (! $rst) {
	            $this->error($validate->getError());
	        } else {
	        	if (input('post.id')) {
					$rst = model('Goods')->editInfo($post);
				} else {
					$rst = model('Goods')->addInfo($post);
				}
	        }
	        return $rst;
		}
		$goodsInfo['goods_sku'] = [];
		if(input('get.id')){
			$goodsInfo = model('Goods')->getInfo(input('get.id'));
		}
		$this->assign('info', $goodsInfo);

		$cate = db('goods_cate')->where('pid',0)->select();
		// foreach ($cate as $k => $v) {
		// 	$cate[$k]['son'] = db('goods_cate')->where('pid',$v['id'])->select();
		// }

		$this->assign('cate',$cate);
		return $this->fetch();
    }

    // 移除sku
    public function rmSku()
    {
    	$skuid = input('get.skuid');

    	if (db('goods_sku')->where('sku_id', $skuid)->delete()) {
    		$this->success('已移除');
    	}

    	$this->error('稍后重试');
    }
    
    // 商城头图
	public function mallPic()
	{
		if(is_post()){
			if(input('get.act') == 'set'){
				db('mallpic')->where('id','not in',input('post.ids/a'))->setField('on',0);
				if(db('mallpic')->where('id','in',input('post.ids/a'))->setField('on',1)){
					$this->success('设置成功');
				}
				$this->error('设置失败');
			}

			if(input('get.act') == 'del'){
				$map['id'] = array('in',input('post.ids/a'));
				if(db('mallpic')->where($map)->setField('status',0)){
					$this->success('删除成功');
				}
				$this->error('操作失败');
			}

			$file = request()->file('titlepic');
	        $info = $file->validate(['ext'=>'jpg,png,gif'])->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'upload/title');
	        if($info){
	       		$data = [
					'pic_name'    => '/upload/title/'.$info->getSaveName(),
					'create_time' => time()
	       		];
	       		db('mallpic')->insert($data);
	        }else{
	            $this->error($file->getError());
	        }
		}
		$this->assign('piclist',db('mallpic')->where('status',1)->order('create_time desc')->select());
		return $this->fetch();
	}

	// 移除商家
	public function rmShop()
	{
		if (is_post()) {
			$shopid = input('post.uid');
			// 商家全部商品下架
			db('goods')->where('shop_id', $shopid)->setField('status', -1);
			db('user')->where('uid', $shopid)->setField('is_shop', 0);
			
			$this->success('商家移除成功！');
		}
	}

	// 商城订单
	public function orderList()
	{
		if (is_post()) {
			// 确认收货
			if (input('get.act') == 'confirm') {
				return model('Order')->confirm(input('post.id'), session('ad_name'));
			}
			// 系统取消订单
			if (input('get.act') == 'cancle') {
				return model('Order')->reject(input('post.id'), session('ad_name'));
			}
		}
		$map = [];
		$map['status'] = ['>',0];
		$where = [];
		if (is_get()) {
			if (is_numeric(input('get.status'))) {
				$map['status'] = input('get.status');
			}
			if (is_numeric(input('get.is_pay'))) {
				$map['is_pay'] = input('get.is_pay');
			}
			if (input('get.keywords')) {
				$where['name'] = ['like', '%'.input('get.keywords').'%'];
			} 
			if (input('get.shop_id')) {
				$where['shop_id'] = input('get.shop_id');
			} 
		}

		$list = model('Order')->getList($map);
		$this->assign('list', $list);

		return $this->fetch();
	}

	// 订单详情
	public function orderDetail()
	{
		if (input('get.id')) {

			$orderId = input('get.id');

			$info = model('Order')->getInfo($orderId);
			$this->assign('info',$info);
		}
		return $this->fetch();
	}




}
